#pragma once

#include "AboutPropertySheet.h"
#include "SnifferURLDiag.h"
#include "NTray.h"
#include "TARDownView.h"
#include "ItemsCheckDiag.h"
#include "AppConfigDiag.h"
#include "MainDownListView.h"
#include "TreeDownCatalog.h"
#include "FileDownInfoList.h"

#define TA_STR_APPLICATION_NAME		L"TAR Download"

class CMainFrame : public CFrameWindowImpl<CMainFrame>, 
				   public CUpdateUI<CMainFrame>,
				   public CMessageFilter, 
				   public CIdleHandler
{
public:
	DECLARE_FRAME_WND_CLASS(TA_STR_APPLICATION_NAME, IDR_MAINFRAME)

	BEGIN_UPDATE_UI_MAP(CMainFrame)
		UPDATE_ELEMENT(ID_VIEW_TOOLBAR, UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_VIEW_STATUS_BAR, UPDUI_MENUPOPUP)
		UPDATE_ELEMENT(ID_VIEW_DOWNCAT, UPDUI_MENUPOPUP)

		UPDATE_ELEMENT(ID_APP_SHOW_TAWINDOW, UPDUI_MENUPOPUP)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CMainFrame)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_TRAY_MESSAGE_ID, OnTrayNotification)

		COMMAND_ID_HANDLER(ID_ADD_URL_SNIFFER, OnNewSniffer)
		COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
		COMMAND_ID_HANDLER(ID_VIEW_TOOLBAR, OnViewToolBar)
		COMMAND_ID_HANDLER(ID_VIEW_STATUS_BAR, OnViewStatusBar)
		COMMAND_ID_HANDLER(ID_VIEW_DOWNCAT, OnViewDownCat)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER(ID_APP_SHOW_TAWINDOW, OnShowWindow)
		COMMAND_ID_HANDLER(ID_APP_OPTIONS, OnOptions)

		CHAIN_MSG_MAP(CUpdateUI<CMainFrame>)
		CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)

		//Chain Cutom Draw msg to the controls
		CHAIN_MSG_MAP_ALT_MEMBER(m_slvDownList, 1);
		CHAIN_MSG_MAP_ALT_MEMBER(m_lvsFileInfo, 1);
	END_MSG_MAP()

	
public:
	virtual BOOL PreTranslateMessage(MSG* pMsg)
	{
		//We Rout some thread message
		switch(pMsg->message)
		{
		case WM_TA_SNIFFER_LINK_STATUS:
		case WM_DOWNLOADING_STATUS:

			pMsg->hwnd = m_slvDownList.m_hWnd;
			break;
		}

		if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
			return TRUE;

		return FALSE;
	}

	virtual BOOL OnIdle()
	{
		UIUpdateToolBar();
		return FALSE;
	}

public:
	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnFileExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

	LRESULT OnNewSniffer(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnOptions(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnViewToolBar(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnViewStatusBar(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnViewDownCat(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnAppAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnShowWindow(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnTrayNotification(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

public:

	CSplitterWindow			m_clientSplitter;			//Client Total Splitter
	CHorSplitterWindow		m_rightSpliter;				//Right panle splitter
	CSplitterWindow			m_rightdownSpliter;			//Right Down Part Splitter


	CTreeDownCatalog	m_trTaskCatelog;
	CTreeViewCtrlEx		m_trThreadOrFileInfo;

	CMainDownListView	m_slvDownList;
	CFileDownInfoList	m_lvsFileInfo;

	CTARDownView m_TaskProgress;

	CReBarCtrl	m_rbRebar;
	CToolBarCtrl m_tbToolbar;

	CTrayNotifyIcon m_tniTrayIcon;
	HICON			m_hitIcon;
};
